import datetime

from _201900800290_init_ import *
import os
from os import system, name
from prettytable import from_db_cursor, DOUBLE_BORDER

# 当前用户姓名
global current_user
current_user = None
# 是否为管理员
is_admin = False


# 输入自动添加单引号
def input_quotes(s):
    return "'" + input(s) + "'"


# 添加单引号
def add_quotes(s):
    return "'" + s + "'"


# 去掉两边的单引号
def trim_quotes(s):
    return s[1:-1]


# 执行SQL并输出
def table_run_sql_and_print(sql):
    cur.execute(sql)
    # 判断是否有结果
    res = cur.fetchone()
    if res:
        cur.execute(sql)
        table = from_db_cursor(cur)
        table.set_style(DOUBLE_BORDER)
        print(table)
        return True
    else:
        formatePrint("查询结果为空")
        return False


# 清屏并显示欢迎信息
def clear_screen():
    if name == 'nt':
        system('cls')
    else:
        system('clear')
    os.system("color 0A")


def welcome():
    print("\n"+currentUser() + '''
    *******************************************
    *            欢迎来到图书管理系统         *
    *******************************************''')


def currentUser():
    if current_user is None:
        return '''                     请先登录                    '''
    elif is_admin:
        return '''                 欢迎管理员：【%s】                ''' % current_user
    else:
        return '''                 欢迎读者：【%s】                   ''' % current_user


# 登录界面
def loginWindow():
    print('''    *******************************************
    *            1.读者登录                   *
    *            2.管理员登录                 *
    *            3.退出系统                   *
    *******************************************
    ''')
    return input('请输入您的选择：')


# 读者登录
def readerLogin():
    name = input_quotes('请输入用户名：')
    user_pwd = input_quotes('请输入密码：')
    sql = 'select * from reader where name=%s and password=%s' % (name, user_pwd)
    cur.execute(sql)
    res = cur.fetchone()
    if res:
        formatePrint('登录成功')
        global is_admin
        is_admin = False
        global current_user
        current_user = trim_quotes(name)
        return True
    else:
        formatePrint('登录失败')
        return False


# 管理员登录
def adminLogin():
    account = input_quotes('请输入用户名：')
    password = input_quotes('请输入密码：')
    sql = 'select * from admin where account = %s and password = %s'
    cur.execute(sql % (account, password))
    res = cur.fetchone()
    if res:
        formatePrint('登录成功')
        global is_admin
        is_admin = True
        global current_user
        current_user = trim_quotes(account)
        return True
    else:
        formatePrint('登录失败')
        return False


# 管理员界面
def adminMenu():
    print('''
    *******************************************
    *            1.图书管理                   *
    *            2.读者管理                   *
    *            3.借阅管理                   *
    *            4.系统维护                   *
    *            5.退出登录                   *
    *******************************************
    ''')


# 图书管理
def bookManageMenu():
    print('''
    ********************************************
    *            1.添加图书                    *
    *            2.删除图书                    *
    *            3.查询图书                    *
    *            4.修改图书                    *
    *            5.返回上一级                  *
    *******************************************
    ''')


# 读者管理
def readerManageMenu():
    print('''
    *******************************************
    *            1.添加读者                   *
    *            2.删除读者                   *
    *            3.查询读者                   *
    *            4.修改读者                   *
    *            5.读者类型管理               *
    *            6.返回上一级                 *
    *******************************************
    ''')


# 读者类型管理
def readerTypeManageMenu():
    print('''
    *******************************************
    *            1.添加读者类型               *
    *            2.删除读者类型               *
    *            3.查询读者类型               *
    *            4.修改读者类型               *
    *            5.返回上一级                 *
    *******************************************
    ''')


# 借阅管理
def borrowManageMenu():
    print('''
    *******************************************
    *            1.借阅图书                   *
    *            2.归还图书                   *
    *            3.查询借阅记录               *
    *            4.读者充值                   *
    *            5.返回上一级                 *
    *******************************************
    ''')


# 系统维护
def systemManageMenu():
    print('''
    *******************************************
    *            1.添加管理员                 *
    *            2.删除管理员                 *
    *            3.查询管理员                 *
    *            4.修改管理员                 *
    *            5.返回上一级                 *
    *******************************************
    ''')


# 读者界面
def readerMenu():
    print('''
    *******************************************
    *            1.个人信息查询               *
    *            2.图书查询                   *
    *            3.个人借阅查询               *
    *            4.修改密码                   *
    *            5.退出登录                   *
    *******************************************
    ''')


# 个人信息查询
def readerInfo():
    reader_info_sql = "select name 姓名, case when gender=0 then N'女' else N'男' end as 性别, " \
                      "unit 单位, tel 联系方式, balance 余额 from reader where name=%s" % ('\'' + current_user + '\'')
    table_run_sql_and_print(reader_info_sql)


# 修改密码
def changePwd():
    old_pwd = input_quotes('请输入旧密码：')
    new_pwd = input_quotes('请输入新密码：')
    change_pwd_sql = 'update reader set password=%s where name=%s and password=%s' % (
        new_pwd, ('\'' + current_user + '\''), old_pwd)
    cur.execute(change_pwd_sql)
    if cur.rowcount == 1:
        formatePrint('修改成功')
    else:
        formatePrint('修改失败,请检查输入')


def add_book():
    formatePrint('添加图书')
    book_id = input_quotes('请输入图书编号：')
    name = input_quotes('请输入图书名称:')
    author = input_quotes('请输入图书作者:')
    price = input_quotes('请输入图书价格:')
    publish = input_quotes('请输入图书出版社:')
    date = input_quotes('请输入图书出版日期(格式XXXX-XX-XX):')
    summary = input_quotes('请输入图书简介:')
    add_book_sql = 'insert into book(book_id, name,author,publisher,publish_date,price,summary) values (%s,%s,%s,%s,' \
                   '%s,%s,%s)' % (
                       book_id, name, author, publish, date, price, summary)
    try:
        cur.execute(add_book_sql)
        formatePrint('添加成功')
    except:
        formatePrint('添加失败,请检查输入')


def delete_book():
    formatePrint('删除图书')
    book_name = input_quotes('请输入图书名称:')
    search_book_sql = 'select * from book where name=%s' % book_name
    flag = table_run_sql_and_print(search_book_sql)
    if flag:
        delete_book_sql = 'delete from book where name=%s' % book_name
        try:
            cur.execute(delete_book_sql)
            formatePrint('删除成功')
        except:
            formatePrint('删除失败,请先删除该图书的借阅记录')


def update_book():
    formatePrint('修改图书')
    id = input_quotes('请输入图书编号:')
    select_book_by_id_sql = "select book_id 图书编号, name 图书名称, author 作者, publisher 出版社, publish_date 出版时间, price 价格, " \
                            "summary 简介, " \
                            "case when is_borrowed='True' then N'是' else '否' end as 是否被借阅 from book where book_id=%s" % id
    cur.execute(select_book_by_id_sql)
    book_info = cur.fetchone()
    if book_info is None:
        formatePrint('没有该图书')
    else:
        formatePrint('该图书信息如下：')
        table_run_sql_and_print(select_book_by_id_sql)
        name = input_quotes('请输入图书名称:')
        author = input_quotes('请输入图书作者:')
        price = input_quotes('请输入图书价格:')
        publish = input_quotes('请输入图书出版社:')
        date = input_quotes('请输入图书出版日期:')
        summary = input_quotes('请输入图书简介:')
        update_book_sql = 'update book set name=%s,author=%s,publisher=%s,publish_date=%s,price=%s,summary=%s where ' \
                          'book_id=%s' % (name, author, publish, date, price, summary, id)
        try:
            cur.execute(update_book_sql)
            formatePrint('修改成功')
        except:
            formatePrint('修改失败,请检查输入')


def select_book():
    formatePrint('查询图书')
    book_name = input('请输入图书名称:')
    select_book_sql = "select book_id 图书编号, name 图书名称, author 作者, publisher 出版社, publish_date 出版时间, price 价格, " \
                      "summary 简介, " \
                      "case when is_borrowed='True' then N'是' else '否' end as 是否被借阅 from book where name like '%%%s%%'" % book_name
    table_run_sql_and_print(select_book_sql)


def select_borrow():
    formatePrint('借阅查询')
    select_borrow_sql = '''select reader_name 借阅人, book.name 图书名称, borrow_date 借阅时间, 
                    return_date 归还时间, 
                    case when is_overdue='TRUE' then N'是' else N'否' end as 是否逾期,
                    overdue_money 逾期金额,
                    case when is_process='TRUE' then N'已处理' else N'未处理' end as 处理状态
            from borrow,book where book.book_id = borrow.book_id and reader_name=%s''' % ('\'' + current_user + '\'')
    table_run_sql_and_print(select_borrow_sql)


def add_reader():
    formatePrint("添加读者")
    name = input_quotes('请输入读者姓名：')
    gender = input("请输入性别：")
    if gender == '男':
        gender = add_quotes('1')
    elif gender == '女':
        gender = add_quotes('0')
    unit = input_quotes("请输入单位：")
    tel = input_quotes("请输入手机号码：")
    balance = input_quotes("请输入余额：")
    password = input_quotes("请输入密码：")
    add_reader_sql = 'insert into reader values(%s,%s,%s,%s,%s,%s)' % (name, gender, unit, tel, balance, password)
    try:
        cur.execute(add_reader_sql)
        formatePrint("读者添加成功")
        select_role_sql = '''select name 名称, borrow_num_limit 最大借阅数, borrow_date_limit 最大借阅天数 from role'''
        table_run_sql_and_print(select_role_sql)
        role_name = input_quotes("请输入要设置的角色名称：")
        try:
            add_reader_role_sql = 'insert into reader_role values(%s,%s)' % (name, role_name)
            cur.execute(add_reader_role_sql)
            formatePrint("读者角色设置成功")
        except:
            formatePrint("读者角色设置失败")
    except:
        formatePrint("读者添加失败，请检查输入")


def delete_reader():
    formatePrint("删除读者")
    name = input_quotes('请输入读者姓名：')
    select_reader_sql = "select name 姓名," \
                        "case when gender=1 then N'男' else N'女' end 性别," \
                        "role_name 读者类型, unit 单位, tel 手机号码, balance 余额 from reader,reader_role " \
                        "where reader_name = reader.name and name like '%%%s%%'" % name
    flag = table_run_sql_and_print(select_reader_sql)
    if flag:
        delete_reader_sql = 'delete from reader where name = %s' % name
        try:
            cur.execute(delete_reader_sql)
            formatePrint("读者删除成功")
        except:
            formatePrint("读者删除失败，请先删除该读者的角色类型绑定与借阅记录")


def select_reader():
    formatePrint("查询读者")
    name = input('请输入读者姓名：')
    select_reader_sql = "select name 姓名," \
                        "case when gender=1 then N'男' else N'女' end 性别," \
                        "role_name 读者类型, unit 单位, tel 手机号码, balance 余额 from reader,reader_role " \
                        "where reader_name = reader.name and name like '%%%s%%'" % name
    table_run_sql_and_print(select_reader_sql)


def update_reader():
    formatePrint("修改读者")
    name = input_quotes('请输入读者姓名：')
    select_reader_sql = '''select name 姓名, 
        case when gender=1 then N'男' else N'女' end 性别,role_name 读者类型, unit 单位, tel 手机号码, balance 余额 from reader,reader_role where reader_name = reader.name and name = %s''' % name
    flag = table_run_sql_and_print(select_reader_sql)
    if flag:
        gender = input_quotes("请输入性别：")
        unit = input_quotes("请输入单位：")
        tel = input_quotes("请输入手机号码：")
        balance = input_quotes("请输入余额：")
        update_reader_sql = 'update reader set gender=%s,unit=%s,tel=%s,balance=%s' % (gender, unit, tel, balance)
        try:
            cur.execute(update_reader_sql)
            formatePrint("读者修改成功")
            select_role_sql = '''select name 名称, borrow_num_limit 最大借阅数, borrow_date_limit 最大借阅天数 from role'''
            table_run_sql_and_print(select_role_sql)
            role_name = input_quotes("请输入要设置的角色名称：")
            try:
                add_reader_role_sql = 'insert into reader_role values(%s,%s)' % (name, role_name)
                cur.execute(add_reader_role_sql)
                formatePrint("读者角色设置成功")
            except:
                formatePrint("读者角色设置失败")
        except:
            formatePrint("读者修改失败，请检查输入")


def add_readerType():
    formatePrint("添加读者类型")
    name = input_quotes('请输入读者类型：')
    borrow_num = input_quotes("请输入最大借阅数：")
    borrow_date = input_quotes("请输入最大借阅天数：")
    add_readerType_sql = 'insert into role values(%s,%s,%s)' % (name, borrow_num, borrow_date)
    try:
        cur.execute(add_readerType_sql)
        formatePrint("读者类型添加成功")
    except:
        formatePrint("读者类型添加失败，请检查输入")


def delete_readerType():
    formatePrint("删除读者类型")
    name = input_quotes('请输入读者类型：')
    delete_readerType_sql = 'delete from role where name = %s' % name
    try:
        cur.execute(delete_readerType_sql)
        formatePrint("读者类型删除成功")
    except:
        formatePrint("读者类型删除失败，请检查输入")


def select_readerType():
    formatePrint("查询读者类型")
    name = input_quotes('请输入读者类型：')
    select_readerType_sql = "select name 类型名称, borrow_num_limit 最大借阅数, borrow_date_limit 最大借阅天数 from role where name = '%%%s%%'" % name
    table_run_sql_and_print(select_readerType_sql)


def update_readerType():
    formatePrint("修改读者类型")
    name = input_quotes('请输入读者类型：')
    select_readerType_sql = '''select name 类型名称, borrow_num_limit 最大借阅数, borrow_date_limit 最大借阅天数 from role where name = %s''' % name
    flag = table_run_sql_and_print(select_readerType_sql)
    if flag:
        borrow_num = input_quotes("请输入最大借阅数：")
        borrow_date = input_quotes("请输入最大借阅天数：")
        update_readerType_sql = 'update role set borrow_num_limit=%s,borrow_date_limit=%s where name=%s' % (borrow_num, borrow_date, name)
        try:
            cur.execute(update_readerType_sql)
            formatePrint("读者类型修改成功")
        except:
            formatePrint("读者类型修改失败，请检查输入")


def borrow_book():
    formatePrint("借书")
    name = input_quotes('请输入读者姓名：')
    select_reader_sql = '''select name 姓名, 
            case when gender=1 then N'男' else N'女' end 性别, unit 单位, tel 手机号码, balance 余额 from reader where name = %s''' % name
    flag = table_run_sql_and_print(select_reader_sql)
    if flag:
        select_book_sql = "select book_id 图书编号, name 图书名称, author 作者, publisher 出版社, publish_date 出版时间, price 价格, " \
                          "summary 简介, " \
                          "case when is_borrowed='True' then N'是' else '否' end as 是否被借阅 from book"
        flag = table_run_sql_and_print(select_book_sql)
        if flag:
            book_id = input_quotes("请输入书籍编号：")
            select_is_book_borrowed_sql = "select is_borrowed from book where book_id = %s" % book_id
            cur.execute(select_is_book_borrowed_sql)
            is_borrowed = cur.fetchone()[0]
            if is_borrowed != 'True':
                insert_borrow_sql = 'insert into borrow(reader_name,book_id,borrow_date) values(%s,%s,%s)' % (
                    name, book_id, add_quotes(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
                try:
                    cur.execute(insert_borrow_sql)
                    formatePrint("借阅新增成功")
                except:
                    formatePrint("借阅新增失败，请检查输入")
            else:
                formatePrint("该书已被借阅")


def return_book():
    formatePrint("还书")
    name = input_quotes('请输入读者姓名：')
    select_borrow_sql = '''select reader_name 借阅人, book.name 图书名称, borrow_date 借阅时间, 
                        return_date 归还时间, 
                        case when is_overdue='TRUE' then N'是' else N'否' end as 是否逾期,
                        overdue_money 逾期金额,
                        case when is_process='TRUE' then N'已处理' else N'未处理' end as 处理状态
                from borrow,book where book.book_id = borrow.book_id and reader_name=%s''' % name
    flag = table_run_sql_and_print(select_borrow_sql)
    if flag:
        borrow_info = cur.fetchone()
        borrow_date = borrow_info[2]  # 借阅时间
        book_id = input_quotes("请输入书籍编号：")
        return_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 归还时间
        select_reader_role_sql = '''select name 类型名称, borrow_num_limit 最大借阅数, borrow_date_limit 最大借阅天数 from role where name = (select role_name from reader_role where name = %s)''' % name
        cur.execute(select_reader_role_sql)
        res = cur.fetchone()
        borrow_date_limit = res[2]  # 最大借阅天数
        # 判断是否逾期
        if datetime.datetime.strptime(return_date, '%Y-%m-%d %H:%M:%S') - datetime.datetime.strptime(borrow_date,
                                                                                                     '%Y-%m-%d %H:%M:%S') > datetime.timedelta(
            days=borrow_date_limit):
            # 如果逾期，则计算逾期金额
            # 查询图书价格
            select_book_sql = '''select name 图书名称, price 价格 from book where book_id = %s''' % book_id
            cur.execute(select_book_sql)
            book_info = cur.fetchone()
            overdue_money = book_info[1]
            is_overdue = 'TRUE'
            is_process = 'FALSE'
            # 扣款
            update_reader_sql = 'update reader set balance = balance - %s where name = %s' % (overdue_money, name)
            try:
                cur.execute(update_reader_sql)
                is_process = 'TRUE'
                formatePrint("扣款成功")
            except:
                formatePrint("余额不足，请先充值")
                return
            update_borrow_sql = 'update borrow set return_date=%s,is_overdue=%s,overdue_money=%s,is_process=%s' \
                                ' where reader_name=%s and book_id=%s' % (
                                    return_date, is_overdue, overdue_money, is_process, name, book_id)
        else:
            # 如果未逾期，则不扣款
            is_overdue = 'FALSE'
            is_process = 'TRUE'
            update_borrow_sql = 'update borrow set return_date=%s,is_overdue=%s,is_process=%s' \
                                ' where reader_name=%s and book_id=%s' % (
                                    return_date, is_overdue, is_process, name, book_id)
        try:
            cur.execute(update_borrow_sql)
            formatePrint("还书成功")
        except:
            formatePrint("还书失败，请检查输入")


def select_reader_borrow():
    formatePrint("查询读者借阅信息")
    name = input_quotes('请输入读者姓名：')
    select_reader_borrow_sql = '''select reader_name 借阅人, book.name 图书名称, borrow_date 借阅时间, 
                        return_date 归还时间, 
                        case when is_overdue='TRUE' then N'是' else N'否' end as 是否逾期,
                        overdue_money 逾期金额,
                        case when is_process='TRUE' then N'已处理' else N'未处理' end as 处理状态
                from borrow,book where book.book_id = borrow.book_id and reader_name=%s''' % name
    table_run_sql_and_print(select_reader_borrow_sql)


def recharge():
    formatePrint("充值")
    name = input_quotes('请输入读者姓名：')
    select_reader_sql = '''select name 姓名, 
            case when gender=1 then N'男' else N'女' end 性别, unit 单位, tel 手机号码, balance 余额 from reader where name = %s''' % name
    flag = table_run_sql_and_print(select_reader_sql)
    if flag:
        recharge_money = input_quotes("请输入充值金额：")
        update_reader_sql = 'update reader set balance=balance+%s where name=%s' % (recharge_money, name)
        try:
            cur.execute(update_reader_sql)
            formatePrint("充值成功")
        except:
            formatePrint("充值失败，请检查输入")


def add_admin():
    formatePrint("添加管理员")
    name = input_quotes('请输入管理员账号：')
    password = input_quotes('请输入管理员密码：')
    select_admin_sql = '''select account 管理员姓名 from admin where account = %s''' % name
    cur.execute(select_admin_sql)
    res = cur.fetchone()
    if res:
        formatePrint("管理员账号已存在")
    else:
        insert_admin_sql = 'insert into admin(account,password) values(%s,%s)' % (name, password)
        try:
            cur.execute(insert_admin_sql)
            formatePrint("添加成功")
        except:
            formatePrint("添加失败，请检查输入")


def delete_admin():
    formatePrint("删除管理员")
    name = input_quotes('请输入管理员账号：')
    select_admin_sql = '''select account 管理员姓名 from admin where account = %s''' % name
    flag = table_run_sql_and_print(select_admin_sql)
    if flag:
        delete_admin_sql = 'delete from admin where account=%s' % name
        try:
            cur.execute(delete_admin_sql)
            formatePrint("删除成功")
        except:
            formatePrint("删除失败，请检查输入")


def select_admin():
    formatePrint("查询管理员")
    account = input_quotes('请输入管理员账号：')
    select_admin_sql = "select account 管理员姓名, password 密码 from admin where account ='%%%s%%'" % account
    table_run_sql_and_print(select_admin_sql)


def update_admin():
    formatePrint("修改管理员")
    name = input_quotes('请输入管理员账号：')
    select_admin_sql = '''select account 管理员姓名 from admin where account = %s''' % name
    flag = table_run_sql_and_print(select_admin_sql)
    if flag:
        password = input_quotes('请输入新管理员密码：')
        update_admin_sql = 'update admin set password=%s where account=%s' % (password, name)
        try:
            cur.execute(update_admin_sql)
            formatePrint("修改成功")
        except:
            formatePrint("修改失败，请检查输入")


def goOn():
    system("pause")
    clear_screen()


if __name__ == '__main__':
    # 开启自动提交
    conn.autocommit(True)
    # 使用创建好的数据库
    cur.execute("use " + db_name)
    flag = True
    while True:
        # 清屏
        clear_screen()
        welcome()
        # 显示登录界面
        login = loginWindow()
        # 读者登录
        if login == '1':
            ch = readerLogin()
            goOn()
            if ch:
                # 读者登录成功
                while True:
                    # 显示读者界面
                    welcome()
                    readerMenu()
                    ch = input('请选择：')
                    # 个人信息查询
                    if ch == '1':
                        readerInfo()
                        goOn()
                    #  图书查询
                    elif ch == '2':
                        select_book()
                        goOn()
                    # 借阅查询
                    elif ch == '3':
                        select_borrow()
                        goOn()
                    # 修改密码
                    elif ch == '4':
                        changePwd()
                        goOn()
                    # 退出登录
                    elif ch == '5':
                        current_user = None
                        formatePrint('退出成功')
                        goOn()
                        break
                    else:
                        formatePrint('输入错误')
                        goOn()
            # 读者登录失败
            else:
                continue
        elif login == '2':
            # 管理员登录
            ch = adminLogin()
            goOn()
            # 管理员登录成功
            if ch:
                while True:
                    # 显示管理员界面
                    welcome()
                    adminMenu()
                    ch = input('请选择：')
                    # 图书管理
                    if ch == '1':
                        while True:
                            # 图书管理界面
                            clear_screen()
                            welcome()
                            bookManageMenu()
                            ch = input('请选择：')
                            # 添加图书
                            if ch == '1':
                                add_book()
                                goOn()
                            # 删除图书
                            elif ch == '2':
                                delete_book()
                                goOn()
                            # 查询图书
                            elif ch == '3':
                                select_book()
                                goOn()
                            # 修改图书
                            elif ch == '4':
                                update_book()
                                goOn()
                            # 退出图书管理
                            elif ch == '5':
                                clear_screen()
                                break
                            else:
                                formatePrint('输入错误')
                                goOn()
                    # 读者管理
                    elif ch == '2':
                        while True:
                            clear_screen()
                            welcome()
                            # 读者管理界面
                            readerManageMenu()
                            ch = input('请选择：')
                            # 添加读者
                            if ch == '1':
                                add_reader()
                                goOn()
                            # 删除读者
                            elif ch == '2':
                                delete_reader()
                                goOn()
                            # 查询读者
                            elif ch == '3':
                                select_reader()
                                goOn()
                            # 修改读者
                            elif ch == '4':
                                update_reader()
                                goOn()
                            elif ch == '5':
                                while True:
                                    clear_screen()
                                    welcome()
                                    readerTypeManageMenu()
                                    ch = input('请选择：')
                                    # 添加读者类型
                                    if ch == '1':
                                        add_readerType()
                                        goOn()
                                    # 删除读者类型
                                    elif ch == '2':
                                        delete_readerType()
                                        goOn()
                                    # 查询读者类型
                                    elif ch == '3':
                                        select_readerType()
                                        goOn()
                                    # 修改读者类型
                                    elif ch == '4':
                                        update_readerType()
                                        goOn()
                                    # 退出读者类型管理
                                    elif ch == '5':
                                        clear_screen()
                                        break
                                    else:
                                        formatePrint('输入错误')
                                        goOn()
                            # 退出读者管理
                            elif ch == '6':
                                clear_screen()
                                break
                            else:
                                formatePrint('输入错误')
                                goOn()
                    # 借阅管理
                    elif ch == '3':
                        while True:
                            # 借阅管理界面
                            clear_screen()
                            welcome()
                            borrowManageMenu()
                            ch = input('请选择：')
                            # 借阅图书
                            if ch == '1':
                                borrow_book()
                                goOn()
                            # 还书
                            elif ch == '2':
                                return_book()
                                goOn()
                            # 查询借阅
                            elif ch == '3':
                                select_reader_borrow()
                                goOn()
                            # 读者充值
                            elif ch == '4':
                                recharge()
                                goOn()
                            # 退出借阅管理
                            elif ch == '5':
                                clear_screen()
                                break
                            else:
                                formatePrint('输入错误')
                                goOn()
                    # 系统维护
                    elif ch == '4':
                        while True:
                            # 系统维护界面
                            clear_screen()
                            welcome()
                            systemManageMenu()
                            ch = input('请选择：')
                            # 添加管理员
                            if ch == '1':
                                add_admin()
                                goOn()
                            # 删除管理员
                            elif ch == '2':
                                delete_admin()
                                goOn()
                            # 查询管理员
                            elif ch == '3':
                                select_admin()
                                goOn()
                            # 修改管理员
                            elif ch == '4':
                                update_admin()
                                goOn()
                            # 退出系统维护
                            elif ch == '5':
                                clear_screen()
                                break
                            else:
                                formatePrint('输入错误')
                                goOn()
                    # 退出管理员
                    elif ch == '5':
                        current_user = None
                        is_admin = False
                        clear_screen()
                        break
                    else:
                        formatePrint('输入错误')
                        goOn()
            else:
                formatePrint('登陆失败')
                goOn()
        elif login == '3':
            break
        else:
            formatePrint('输入错误')
            goOn()
    # 关闭数据库连接
    conn.close()
    formatePrint('程序已退出')
    system("pause")
    exit(0)
